#include "collection.h"

Collection::Collection(AudioPlayerBackend * music)
{
    this->music = music;

    QList < QString > tmplist = music->getAllArtists();

    artists.append(new Artist(QString("ALL")));

    QString tmpstr;
    foreach(tmpstr,tmplist){
        artists.append(new Artist(tmpstr));
    }
}

QList< Artist *> * Collection::getArtists()
{
    return &artists;
}

QList< Album *> * Collection::getAlbums(Artist *artist)
{

    if(artist->getAlbums() == NULL){
        QList< QString > tmplist;
        if(artist->getName() == "ALL"){
            tmplist = music->getAllAlbums();
        }else{
            tmplist = music->getAlbumsFromArtist(artist->getName());
        }

        QList< Album* >  * albums = new QList< Album* >();
        QString tmp;
        Album *album = new Album("ALL");
        album->setArtist(artist);
        albums->append( album );
        foreach(tmp,tmplist){
            album = new Album(tmp);
            album->setArtist(artist);
            albums->append( album );
        }
        artist->setAlbums(albums);
    }

    return artist->getAlbums();
}

QList< Song *> * Collection::getSongs(Album * album)
{
    if(album->getSongs() == NULL){
        QList< Song* > * songs;
        if(album->getName() == "ALL"){
            if(album->getArtist()->getName().compare(QString("ALL")) == 0){
                songs = music->getAllSongs();
            }else{
                songs = music->getSongsFromArtist(album->getArtist());
            }
        }else{
            songs = music->getSongsFromAlbum(album);
        }

        album->setSongs(songs);
    }

    return album->getSongs();
}
